/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package control.benhan;

import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import model.dao.addxuonghibernate.PhieuDieuTriDAO;
import model.dao.benhan.BenhAnDAO;
import model.entity.Benh;
import model.entity.BenhAn;
import model.entity.PhieuDieuTri;
import model.entity.VatNuoi;

/**
 *
 * @author Admin
 */
public class BAMainPanel_Ctrl {
    public static final void loadBenhAnLenTable(List<BenhAn> list, JTable tbl){
        Vector<String>tableHeader = new Vector<String>();
        tableHeader.add("Mã Bệnh Án");
        tableHeader.add("Mã Vật Nuôi");
        tableHeader.add("Tên Vật Nuôi");
        tableHeader.add("Mã Bệnh");
        tableHeader.add("Tên Bệnh");
        tableHeader.add("Ngày Bắt Đầu");
        tableHeader.add("Ngày Kết Thúc");
        tableHeader.add("Trạng Thái");
        tableHeader.add("Ghi Chú");
        
        Vector data = new Vector();
        for (Object object : list) {
            BenhAn benhAn = (BenhAn)object;
            
            Vector<Object>oneRow = new Vector<Object>();
            if(benhAn.getBaMa() != null){
                oneRow.add(benhAn.getBaMa());
            }else{
                oneRow.add("");
            }
            if(benhAn.getVatNuoi() != null || benhAn.getVatNuoi().getVnMa() != null){
                oneRow.add(benhAn.getVatNuoi().getVnMa());
            }else{
                oneRow.add("");
            }
            if(benhAn.getVatNuoi() != null || benhAn.getVatNuoi().getVnTen() != null){
                oneRow.add(benhAn.getVatNuoi().getVnTen());
            }else{
                oneRow.add("");
            }
            if(benhAn.getBenh() != null || benhAn.getBenh().getBenhMa() != null){
                oneRow.add(benhAn.getBenh().getBenhMa());
            }else{
                oneRow.add("");
            }
            if(benhAn.getBenh() != null || benhAn.getBenh().getBenhTen() != null){
                oneRow.add(benhAn.getBenh().getBenhTen());
            }else{
                oneRow.add("");
            }
            if(benhAn.getBaNgayBd() != null){
                oneRow.add(benhAn.getBaNgayBd());
            }else{
                oneRow.add("");
            }
            if(benhAn.getBaNgayKt() != null){
                oneRow.add(benhAn.getBaNgayKt());
            }else{
                oneRow.add("");
            }
            if(benhAn.getBaTrangThai() != null){
                oneRow.add(benhAn.getBaTrangThai());
            }else{
                oneRow.add("");
            }
            if(benhAn.getBaGhiChu() != null){
                oneRow.add(benhAn.getBaGhiChu());
            }else{
                oneRow.add("");
            }
            
            
            data.add(oneRow);
        }
        
        tbl.setModel(new DefaultTableModel(data, tableHeader));
        tbl.resetKeyboardActions();
    }
     public static final void loadPhieuDieuTriLenTable(JTable table, List<PhieuDieuTri> list){
        
        Vector<String> tableHeader = new Vector<String>();
        tableHeader.add("Mã Phiếu Điều Trị");
//        tableHeader.add("Tên Bệnh");
        tableHeader.add("Ngày Điều Trị");
        tableHeader.add("Ngày Tái Khám");
        
        Vector data = new Vector();
        
        for (Object object : list) {
            PhieuDieuTri pdt = (PhieuDieuTri)object;
            Vector<Object>oneRow = new Vector<Object>();
            
            oneRow.add(pdt.getPdtMa());
//            oneRow.add(pdt.getBenhAn().getBenh().getBenhTen());
            oneRow.add(pdt.getPdtNgayDieuTri());
            oneRow.add(pdt.getPdtNgayTaiKham());
            
            data.add(oneRow);
        }
        
        table.setModel(new DefaultTableModel(data, tableHeader));
    }
     public static void loadAllPhieuDiauTriTheoMa(JTable table, int maBenhAn){
         List<PhieuDieuTri> list = PhieuDieuTriDAO.timPhieuDieuTriTheoMaHSBA(maBenhAn);
         loadPhieuDieuTriLenTable(table, list);
         table.resetKeyboardActions();
     }
    /**
     * Load tất cả bệnh án
     * @param tbl 
     */
    public static final void LoadAllBenhAn(JTable tbl){
        List<BenhAn> resultList = BenhAnDAO.loaddAllBenhAn();
        loadBenhAnLenTable(resultList, tbl);
    } 
    
    
    
    public static final void lietKeTheoTenBA(JTextField tfTen, JTable table){
        String tenB = tfTen.getText();
        List list = BenhAnDAO.loadBenhAnTheoTenBenh(tenB);
        loadBenhAnLenTable(list, table);
        
    }
    
    /**
     * 
     */
     public static final void lietKeTheoMaBA(JTextField tfMa, JTable table){
        int maBA = Integer.parseInt(tfMa.getText());
        List list = BenhAnDAO.loadBenhAnTheoMaBenhAn(maBA);
        loadBenhAnLenTable(list, table);
        
    }
    /**
     * 
     */
    
    public static final BenhAn selectRow(JTable table){
        int row = table.getSelectedRow();
        
        int maBA = (Integer)table.getValueAt(row, 0);
        int maVatNuoi = (Integer) table.getValueAt(row, 1);
        String tenVatNuoi = (String) table.getValueAt(row, 2);
        int maBenh = (Integer) table.getValueAt(row, 3);
        String tenBenh = (String) table.getValueAt(row, 4);
        Date ngayBD = (Date) table.getValueAt(row, 5);
        Date ngayKT = (Date)table.getValueAt(row, 6);
        String trangThai = (String)table.getValueAt(row, 7);
        String ghiChu = (String)table.getValueAt(row, 8);
        System.out.println(trangThai);
        BenhAn benhAn = new BenhAn();
        Benh benh = new Benh();
        VatNuoi vatNuoi = new VatNuoi();
        
        vatNuoi.setVnMa(maVatNuoi);
        vatNuoi.setVnTen(tenVatNuoi);
        benh.setBenhMa(maBenh);
        benh.setBenhTen(tenBenh);
        
        benhAn.setBaMa(maBA);
        benhAn.setBenh(benh);
        benhAn.setBaNgayBd(ngayBD);
        benhAn.setBaNgayKt(ngayKT);
        benhAn.setBaTrangThai(trangThai);
        benhAn.setBaGhiChu(ghiChu);
        
        return benhAn;
        
    }
    
    /**
     * Load Phieu Dieu Tri Theo Ma hồ sơ Bệnh án
     */
    public static final void loadPHieuDieuTriTheoMaHSBA(JTable table, int maBA){
        
        Vector<String> tableHeader = new Vector<String>();
        tableHeader.add("Mã Phiếu Điều Trị");
//        tableHeader.add("Tên Bệnh");
        tableHeader.add("Ngày Điều Trị");
        tableHeader.add("Ngày Tái Khám");
        
        List list = PhieuDieuTriDAO.timPhieuDieuTriTheoMaHSBA(maBA);
        Vector data = new Vector();
        
        for (Object object : list) {
            PhieuDieuTri pdt = (PhieuDieuTri)object;
            Vector<Object>oneRow = new Vector<Object>();
            oneRow.add(pdt.getPdtMa());
//            oneRow.add(pdt.getBenhAn().getBenh().getBenhTen());
            oneRow.add(pdt.getPdtNgayDieuTri());
            oneRow.add(pdt.getPdtNgayTaiKham());
            
            data.add(oneRow);
        }
        
        table.setModel(new DefaultTableModel(data, tableHeader));
    }
}
